import { reactive } from 'vue'
import request from '@/js/request.js';

// 防抖函数
export function debounce(func, delay) {
    let timeout;
    return function (...args) {
        clearTimeout(timeout);
        timeout = setTimeout(() => {
            func.apply(this, args);
        }, delay||500);
    };
}

// 登录数据接口
export const login_api = reactive({
    api: '/login',
    post(data){
        return request({
            url: this.api,
            method: 'post',
            data: data
        })
    },
})

// 首页数据接口
export const home_api = reactive({
    api: '/home',
    get(query){
        return request({
            url: this.api,
            method: 'get',
            params: query,
            verify: true //验证用户token
        })
    },
    add(data){
        return request({
            url: this.api,
            method: 'post',
            data: data,
            verify: true
        })
    },
    edit(data){
        return request({
            url: this.api + '/' + data.id,
            method: 'put',
            data: data,
            verify: true
        })
    },
    del(id){
        return request({
            url: this.api + '/' + id,
            method: 'delete',
            verify: true
        })
    }
})

// 首页数据接口
export const diy_api = reactive({
    api: '/js/diy.json',
    get(query){
        return request({
            url: this.api,
            method: 'get',
            params: query,
            verify: true //验证用户token
        })
    },
    add(data){
        return request({
            url: this.api,
            method: 'post',
            data: data,
            verify: true
        })
    },
    edit(data){
        return request({
            url: this.api + '/' + data.id,
            method: 'put',
            data: data,
            verify: true
        })
    },
    del(id){
        return request({
            url: this.api + '/' + id,
            method: 'delete',
            verify: true
        })
    }
})
